$(function () {
    var url = "/except/shopcontroller/ichibanbanner.do";
    var token = $("#token").val();
    if(isempty(token))
        token="null";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json:jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            var appendstr = '';
            if (code == 20013)
                $("#banner").before('<img src="img/DSC_1476.jpg" class="img_pages"/>');
            else if (code == 20000) {
                var datas = data.data;
                $(datas).each(function () {
                    $("#banner").before('<img src="' + this.img_address + '" class="img_pages" onerror="imgerror(this,\'img/onerror.png\');"/>');
                });
            } else{
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
            // $("#banner").empty().append(appendstr);
            storymoreinfo(0);
            remove_advertising();
        },
        error: function (data) {
            $("#hintbg").show()
            $("#hint_message").html("网络错误");
        }
    });
});

function storymoreinfo(page) {
    url = "/except/shopcontroller/homepagestory.do";
    var token = $("#token").val();
    if(isempty(token))
        token="null";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            page: page,
            count: 10,
            type:1,
            json:jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            var storyinfo = "";
            if (code == 20000) {
                var datas = data.data;
                $(datas).each(function () {
                    var address = this.story_logo;
                    storyinfo += '<div class="item_div" onclick="storyinfo(\'' + this.story_id + '\', ' + this.type + ', \'' + this.shopid + '\');">' +
                        '<div style="border-radius: 6px;margin-bottom: 100px; height: 200px;overflow-y: hidden; background: url('+address+') no-repeat center; background-size: cover;width: 92%;margin: 0 auto;"onerror="imgerror(this,\'img/onerror.png\');"></div> '+
                        // '<img class="item_img"  src="' + this.story_logo + '" onerror="imgerror(this,\'img/onerror.png\');"/>' +
                        '<span class="item_span_storename">' + this.story_name + '</span>' +
                        '<span class="item_span_desc">' + this.brief + '</span>' +
                        '</div>';
                });
                if (page == 0)
                    $("#storyinfo").empty().append(storyinfo);
                else
                    $("#storyinfo").append(storyinfo);
                $("#page").val(page * 1 + 1);
            } else if (code == 20013){
                if (page == 0){
                    $("#not_data").show();
                }else{
                    $("#nomore").show();
                }
            }else {
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
        }
    });
}

$(window).scroll(function () {
    //$(document).scrollTop() 获取垂直滚动的距离
    //$(document).scrollLeft() 这是获取水平滚动条的距离
    if ($(document).scrollTop() <= 0) {
        storymoreinfo(0);
    }
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        var page = $("#page").val();
        storymoreinfo(page);
    }
});

function storyinfo(storyid, type, shopid) {
    if (type == "1")
        location.href = "/except/files/shop_story.jsp?storyid=" + storyid + "&shopid=" + shopid;
    else
        location.href = "/except/files/story.jsp?storyid=" + storyid ;
}